package com.example.android20191014test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {


    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //点
        canvas.drawColor(Color.parseColor("#FFF000"));
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#f00000"));
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(50);
        canvas.drawPoint(200, 200, paint);


        //矩形
        RectF rectF=new RectF(100,100,400,600);
        paint.setColor(Color.parseColor("#005678"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        canvas.drawRect(rectF, paint);


        //带弧形的矩形
        RectF rectCircle=new RectF(50,50,450,650);
        paint.setColor(Color.parseColor("#005678"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        canvas.drawRoundRect(rectCircle,50,50,paint);


        //椭圆
        RectF rectFCircle=new RectF(300,200,700,800);
        paint.setColor(Color.parseColor("#005678"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        canvas.drawRoundRect(rectFCircle,500,750,paint);

        RectF rectF1=new RectF(400,200,600,800);
        paint.setColor(Color.parseColor("#005678"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        canvas.drawOval(rectF1,paint);


        //圆
        paint.setColor(Color.parseColor("#005678"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        canvas.drawCircle(500,500,300,paint);


        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.nv);
        float viewWidth=getWidth();
        float bitmapWidth=bitmap.getWidth();
        float bitmapHeight=bitmap.getHeight();
        float showHeight=(viewWidth/bitmapWidth)*bitmapHeight;
        canvas.drawBitmap(bitmap,null,new RectF(0,0,viewWidth,showHeight),paint);
    }
}
